package com.fzy.core.utils;

public class StringUtils extends org.apache.commons.lang.StringUtils {
	public static String unescape(String src)
	{
		if(src == null) return null;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length())
		{
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos)
			{
				if (src.charAt(pos + 1) == 'u')
				{
					ch = (char) Integer.parseInt(src
							.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				}
				else
				{
					ch = (char) Integer.parseInt(src
							.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			}
			else
			{
				if (pos == -1)
				{
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				}
				else
				{
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	/**
	 * <p>
	 * Check if a String starts with a specified prefix.
	 * </p>
	 * 
	 * <p>
	 * <code>null</code>s are handled without exceptions. Two
	 * <code>null</code> references are considered to be equal. The comparison
	 * is case sensitive.
	 * </p>
	 * 
	 * <pre>
	 *  StringUtils.startsWith(null, null)      = true
	 *  StringUtils.startsWith(null, &quot;abc&quot;)     = false
	 *  StringUtils.startsWith(&quot;abcdef&quot;, null)  = false
	 *  StringUtils.startsWith(&quot;abcdef&quot;, &quot;abc&quot;) = true
	 *  StringUtils.startsWith(&quot;ABCDEF&quot;, &quot;abc&quot;) = false
	 * </pre>
	 * 
	 * @see java.lang.String#startsWith(String)
	 * @param str
	 *            the String to check, may be null
	 * @param prefix
	 *            the prefix to find, may be null
	 * @return <code>true</code> if the String starts with the prefix, case
	 *         sensitive, or both <code>null</code>
	 * @since 2.4
	 */
	public static boolean startsWith(String str, String prefix) {
		return startsWith(str, prefix, false);
	}

	/**
	 * <p>
	 * Case insensitive check if a String starts with a specified prefix.
	 * </p>
	 * 
	 * <p>
	 * <code>null</code>s are handled without exceptions. Two
	 * <code>null</code> references are considered to be equal. The comparison
	 * is case insensitive.
	 * </p>
	 * 
	 * <pre>
	 *  StringUtils.startsWithIgnoreCase(null, null)      = true
	 *  StringUtils.startsWithIgnoreCase(null, &quot;abc&quot;)     = false
	 *  StringUtils.startsWithIgnoreCase(&quot;abcdef&quot;, null)  = false
	 *  StringUtils.startsWithIgnoreCase(&quot;abcdef&quot;, &quot;abc&quot;) = true
	 *  StringUtils.startsWithIgnoreCase(&quot;ABCDEF&quot;, &quot;abc&quot;) = true
	 * </pre>
	 * 
	 * @see java.lang.String#startsWith(String)
	 * @param str
	 *            the String to check, may be null
	 * @param prefix
	 *            the prefix to find, may be null
	 * @return <code>true</code> if the String starts with the prefix, case
	 *         insensitive, or both <code>null</code>
	 * @since 2.4
	 */
	public static boolean startsWithIgnoreCase(String str, String prefix) {
		return startsWith(str, prefix, true);
	}

	/**
	 * <p>
	 * Check if a String starts with a specified prefix (optionally case
	 * insensitive).
	 * </p>
	 * 
	 * @see java.lang.String#startsWith(String)
	 * @param str
	 *            the String to check, may be null
	 * @param prefix
	 *            the prefix to find, may be null
	 * @param ignoreCase
	 *            inidicates whether the compare should ignore case (case
	 *            insensitive) or not.
	 * @return <code>true</code> if the String starts with the prefix or both
	 *         <code>null</code>
	 */
	private static boolean startsWith(String str, String prefix,
			boolean ignoreCase) {
		if (str == null || prefix == null) {
			return (str == null && prefix == null);
		}
		if (prefix.length() > str.length()) {
			return false;
		}
		return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
	}
	
	public static boolean hasText(CharSequence str) {
		if (!hasLength(str)) {
			return false;
		}
		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}
	public static boolean hasLength(CharSequence str) {
		return (str != null && str.length() > 0);
	}
}
